package cn.stylefeng.guns.modular.business.frequency.financesettlementimage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.business.frequency.financesettlementimage.entity.FinanceSettlementImage;
import cn.stylefeng.guns.modular.business.frequency.financesettlementimage.mapper.FinanceSettlementImageMapper;
import cn.stylefeng.guns.modular.business.frequency.financesettlementimage.model.param.FinanceSettlementImageParam;
import cn.stylefeng.guns.modular.business.frequency.financesettlementimage.service.FinanceSettlementImageService;
import cn.stylefeng.guns.modular.business.frequency.financesettlementimage.enums.FinanceSettlementImageExceptionEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 订单财务结算图片 服务实现类
 *
 * @author yujie
 * @date 2023/05/09 17:17
 */
@Service
public class FinanceSettlementImageServiceImpl extends ServiceImpl<FinanceSettlementImageMapper, FinanceSettlementImage> implements FinanceSettlementImageService {

    @Override
    public PageResult<FinanceSettlementImage> page(FinanceSettlementImageParam financeSettlementImageParam) {

        // 构造条件
        LambdaQueryWrapper<FinanceSettlementImage> queryWrapper = new LambdaQueryWrapper<>();

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<FinanceSettlementImage> list(FinanceSettlementImageParam financeSettlementImageParam) {

        // 构造条件
        LambdaQueryWrapper<FinanceSettlementImage> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(FinanceSettlementImageParam financeSettlementImageParam) {

        // 将dto转为实体
        FinanceSettlementImage financeSettlementImage = new FinanceSettlementImage();
        BeanUtil.copyProperties(financeSettlementImageParam, financeSettlementImage);

        this.save(financeSettlementImage);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(FinanceSettlementImageParam financeSettlementImageParam) {
        this.removeById(financeSettlementImageParam.getId());
    }

    @Override
    public void edit(FinanceSettlementImageParam financeSettlementImageParam) {

        // 根据id查询实体
        FinanceSettlementImage financeSettlementImage = this.queryFinanceSettlementImage(financeSettlementImageParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(financeSettlementImageParam, financeSettlementImage);

        this.updateById(financeSettlementImage);
    }

    @Override
    public FinanceSettlementImage detail(FinanceSettlementImageParam financeSettlementImageParam) {
        return this.queryFinanceSettlementImage(financeSettlementImageParam);
    }



    /**
     * 获取订单财务结算图片
     *
     * @author yujie
     * @since 2023/05/09 17:17
     */
    private FinanceSettlementImage queryFinanceSettlementImage(FinanceSettlementImageParam financeSettlementImageParam) {
        FinanceSettlementImage financeSettlementImage = this.getById(financeSettlementImageParam.getId());
        if (ObjectUtil.isEmpty(financeSettlementImage)) {
            throw new ServiceException(FinanceSettlementImageExceptionEnum.NOT_EXISTED);
        }
        return financeSettlementImage;
    }
}